<?php
namespace app\task\logic;

class WrFormDepartmentDayLogic extends BaseLogic{

    /**
     * 获取需要计算的时间
     * @param $department
     * @return mixed
     */
    public function get_count_time($department){
        $maps = [];
        $maps[] = ['department','=',$department];
        $wrfd_detail = model('wr_form_department_day')->get_detail($maps,'id,timeid','timeid desc');
        if(!empty($wrfd_detail)){
            $timeid = $wrfd_detail['timeid'];

            $date = date('Y-m-d H:i:s',strtotime('+1 day',strtotime($timeid)));

            //获取指定日期所在天的开始时间和结束时间
            $r = get_the_today_time($date);

            $start_time = $r['start_time'];
            $end_time = $r['end_time'];
        }else{
            $start_time = '2019-11-01 00:00:00';
            $end_time = '2019-11-01 23:59:59';
        }

        $now_time = date('Y-m-d H:i:s');
        if($now_time < $end_time){
            return $this->get_response(0,'还未到达计算时间');
        }

        $data['start_time'] = $start_time;
        $data['end_time'] = $end_time;
        $data['timeid'] = date('Y-m-d',strtotime($start_time));
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 计算发布次数
     * @param $department
     * @param $start_time
     * @param $end_time
     * @return mixed
     */
    public function count_release_num($department,$start_time,$end_time){
        $maps = [];
        $maps[] = ['department','=',$department];
        $openids = model('user')->get_column($maps,'openid');

        //推广工作发布次数
        $maps = [];
        $maps[] = ['openid','in',$openids];
        $maps[] = ['c_time','between',[$start_time,$end_time]];
        $extend_release_num = model('work_record_extend')->get_count($maps,'id');

        //工作日志发布次数
        $maps = [];
        $maps[] = ['openid','in',$openids];
        $maps[] = ['c_time','between',[$start_time,$end_time]];
        $daily_release_num = model('work_record_daily')->get_count($maps,'id');

        //活动开展发布次数
        $maps = [];
        $maps[] = ['openid','in',$openids];
        $maps[] = ['c_time','between',[$start_time,$end_time]];
        $active_release_num = model('work_record_active')->get_count($maps,'id');

        //通知公告发布次数
        $maps = [];
        $maps[] = ['openid','in',$openids];
        $maps[] = ['c_time','between',[$start_time,$end_time]];
        $notice_release_num = model('work_record_notice')->get_count($maps,'id');

        //每日数据发布次数
        $start_date = date('Y-m-d',strtotime($start_time));
        $end_date = date('Y-m-d',strtotime($end_time));
        $maps = [];
        $maps[] = ['openid','in',$openids];
        $maps[] = ['now_date','between',[$start_date,$end_date]];
        $data_release_num = model('work_record_data')->get_count($maps,'id');

        $data['extend_release_num'] = $extend_release_num;
        $data['daily_release_num'] = $daily_release_num;
        $data['active_release_num'] = $active_release_num;
        $data['notice_release_num'] = $notice_release_num;
        $data['data_release_num'] = $data_release_num;
        return $data;
    }
}